import Vue from "vue";
import XEUtils from "xe-utils";
// 完整导入 UI 组件库
import VxeUI from "vxe-pc-ui";
import "vxe-pc-ui/lib/style.css";

// 完整导入 表格库
import VxeUITable from "vxe-table";
import "vxe-table/lib/style.css";

Vue.use(VxeUI);
Vue.use(VxeUITable);
import VXETablePluginAntd from "vxe-table-plugin-antd";

import zhCN from "vxe-table/lib/locale/lang/zh-CN";

VxeUI.setConfig({
  validFullData: "obsolete", // 将默认为校验全量数据
  i18n: (key, args) => XEUtils.toFormatString(XEUtils.get(zhCN, key), args),
  size: "small", // 全局尺寸
  zIndex: 999, // 全局 zIndex 起始值，如果项目的的 z-index 样式值过大时就需要跟随设置更大，避免被遮挡
  version: 1.0, // 版本号，对于某些带数据缓存的功能有用到，上升版本号可以用于重置数据
  table: {
    showHeader: true,
    keepSource: false,
    showOverflow: "tooltip",
    animat: true,
    cloak: false,
    delayHover: 250,
    showHeaderOverflow: null,
    showFooterOverflow: null,
    size: null,
    resizable: true,
    autoResize: false,
    stripe: false,
    border: true,
    columnConfig: {
      isCurrent: true,
      isHover: true,
      useKey: true,
      drag: true,
      maxFixedSize: 20,
    },
    rowConfig: {
      isCurrent: true,
      isHover: true,
      useKey: true,
    },
    //   round: false,
    emptyText: "暂无数据",
    radioConfig: {
      trigger: "row",
    },
    checkboxConfig: {
      trigger: "row",
    },
    //   sortConfig: {
    //     remote: false,
    //     trigger: 'default',
    //     orders: ['asc', 'desc', null],
    //     sortMethod: null
    //   },
    //   filterConfig: {
    //     remote: false,
    //     filterMethod: null
    //   },
    //   expandConfig: {
    //     trigger: 'default',
    //     showIcon: true
    //   },
    //   treeConfig: {
    //     children: 'children',
    //     hasChild: 'hasChild',
    //     indent: 20,
    //     showIcon: true
    //   },
    printConfig: {},
    tooltipConfig: {
      showAll: true,
      enterable: true,
      contentMethod: ({ type, column, row, items, _columnIndex }) => {
        const { field, title } = column;
        if (title == "操作") {
          return "";
        }
        return null;
      },
    },
    //   menuConfig: {
    //     visibleMethod () {}
    //   },
    //   rowId: '_XID', // 行数据的唯一记录ID字段名
    //   editConfig: {
    //     mode: 'cell',
    //     showAsterisk: true
    //   },
    exportConfig: {
      modes: ["current", "selected"],
    },
    customConfig: {
      storage: true,
      mode: "popup",
    },
    scrollX: {
      enabled: true,
      gt: 0,
    },
    scrollY: {
      enabled: true, //是否启用
      gt: 0,
    },
  },
  grid: {
    size: null,
    border: true,
    resizable: true,
    zoomConfig: {
      escRestore: true,
    },

    rowConfig: { useKey: true },
    columnConfig: {
      useKey: true,
      drag: true,
      maxFixedSize: 20,
    },
    filterConfig: {
      remote: true,
    },
    pagerConfig: {
      perfect: false,
      pageSize: 20,
      pageSizes: [10, 20, 50, 100, 200, 500, 1000],
    },
    toolbarConfig: {
      refresh: {
        icon: "vxe-icon-refresh",
        iconLoading: "vxe-icon-spinner roll",
      },
      import: false,
      export: {
        icon: "vxe-icon-download",
      },
      print: {
        icon: "vxe-icon-print",
      },
      zoom: {
        iconIn: "vxe-icon-fullscreen",
        iconOut: "vxe-icon-minimize",
      },
      custom: {
        icon: "vxe-icon-menu",
      },
    },

    toolbar: {
      size: null,
      import: {
        mode: "covering",
      },
      export: {
        types: ["csv", "html", "xml", "txt"],
      },
      custom: {
        isFooter: true,
      },
      buttons: [],
      tools: [],
    },
    button: {
      size: null,
      transfer: false,
    },
    radio: {
      size: null,
    },
    checkbox: {
      size: null,
    },
    switch: {
      size: null,
    },
    scrollX: {
      enabled: true,
      gt: 0,
    },
    scrollY: {
      enabled: true,
      gt: 0,
    },

    customConfig: {
      storage: true,
      mode: "popup",
    },
    list: {
      scrollY: {
        gt: 100,
      },
    },
  },
  pager: {
    size: null,
    autoHidden: false,
    perfect: true,
    pageSize: 10,
    pagerCount: 7,
    pageSizes: [10, 15, 20, 50, 100],
    layouts: [
      "Home",
      "PrevJump",
      "PrevPage",
      "JumpNumber",
      "NextPage",
      "NextJump",
      "End",
      "Sizes",
      "FullJump",
      "Total",
    ],
  },
  icon: {
    // loading
    LOADING: "vxe-icon-spinner roll vxe-loading--default-icon",

    // table
    TABLE_SORT_ASC: "vxe-icon-caret-up",
    TABLE_SORT_DESC: "vxe-icon-caret-down",
    TABLE_FILTER_NONE: "vxe-icon-funnel",
    TABLE_FILTER_MATCH: "vxe-icon-funnel",
    TABLE_EDIT: "vxe-icon-edit",
    TABLE_HELP: "vxe-icon-question-circle-fill",
    TABLE_TREE_LOADED: "vxe-icon-spinner roll",
    TABLE_TREE_OPEN: "vxe-icon-caret-right rotate90",
    TABLE_TREE_CLOSE: "vxe-icon-caret-right",
    TABLE_EXPAND_LOADED: "vxe-icon-spinner roll",
    TABLE_EXPAND_OPEN: "vxe-icon-arrow-right rotate90",
    TABLE_EXPAND_CLOSE: "vxe-icon-arrow-right",
    TABLE_CHECKBOX_CHECKED: "vxe-icon-checkbox-checked",
    TABLE_CHECKBOX_UNCHECKED: "vxe-icon-checkbox-unchecked",
    TABLE_CHECKBOX_INDETERMINATE: "vxe-icon-checkbox-indeterminate",
    TABLE_RADIO_CHECKED: "vxe-icon-radio-checked",
    TABLE_RADIO_UNCHECKED: "vxe-icon-radio-unchecked",

    // button
    BUTTON_DROPDOWN: "vxe-icon-arrow-down",
    BUTTON_LOADING: "vxe-icon-spinner roll",

    // select
    SELECT_LOADED: "vxe-icon-spinner roll",
    SELECT_OPEN: "vxe-icon-caret-down rotate180",
    SELECT_CLOSE: "vxe-icon-caret-down",

    // pager
    PAGER_JUMP_PREV: "vxe-icon-arrow-double-left",
    PAGER_JUMP_NEXT: "vxe-icon-arrow-double-right",
    PAGER_PREV_PAGE: "vxe-icon-arrow-left",
    PAGER_NEXT_PAGE: "vxe-icon-arrow-right",
    PAGER_JUMP_MORE: "vxe-icon-ellipsis-h",

    // input
    INPUT_CLEAR: "vxe-icon-error-circle-fill",
    INPUT_PWD: "vxe-icon-eye-fill",
    INPUT_SHOW_PWD: "vxe-icon-eye-fill-close",
    INPUT_PREV_NUM: "vxe-icon-caret-up",
    INPUT_NEXT_NUM: "vxe-icon-caret-down",
    INPUT_DATE: "vxe-icon-calendar",
    INPUT_SEARCH: "vxe-icon-search",

    // modal
    MODAL_ZOOM_IN: "vxe-icon-square",
    MODAL_ZOOM_OUT: "vxe-icon-maximize",
    MODAL_CLOSE: "vxe-icon-close",
    MODAL_INFO: "vxe-icon-info-circle-fill",
    MODAL_SUCCESS: "vxe-icon-success-circle-fill",
    MODAL_WARNING: "vxe-icon-warnion-circle-fill",
    MODAL_ERROR: "vxe-icon-error-circle-fill",
    MODAL_QUESTION: "vxe-icon-question-circle-fill",
    MODAL_LOADING: "vxe-icon-spinner roll",

    // toolbar
    TOOLBAR_TOOLS_REFRESH: "vxe-icon-repeat",
    TOOLBAR_TOOLS_REFRESH_LOADING: "vxe-icon-repeat roll",
    TOOLBAR_TOOLS_IMPORT: "vxe-icon-upload",
    TOOLBAR_TOOLS_EXPORT: "vxe-icon-download",
    TOOLBAR_TOOLS_PRINT: "vxe-icon-print",
    TOOLBAR_TOOLS_FULLSCREEN: "vxe-icon-fullscreen",
    TOOLBAR_TOOLS_MINIMIZE: "vxe-icon-minimize",
    TOOLBAR_TOOLS_CUSTOM: "vxe-icon-custom-column",

    // form
    FORM_PREFIX: "vxe-icon-question-circle-fill",
    FORM_SUFFIX: "vxe-icon-question-circle-fill",
    FORM_FOLDING: "vxe-icon-arrow-up rotate180",
    FORM_UNFOLDING: "vxe-icon-arrow-up",
  },
});

// 自定义全局的格式化处理函数
VxeUI.formats.mixin({
  // 格式化性别
  formatSex({ cellValue }) {
    return cellValue ? (cellValue === "1" ? "男" : "女") : "";
  },
  // 格式化下拉选项
  formatSelect({ cellValue }, list) {
    const item = list.find((item) => item.value === cellValue);
    return item ? item.label : "";
  },
  // 格式化日期，默认 yyyy-MM-dd HH:mm:ss
  formatDate({ cellValue }, format) {
    return XEUtils.toDateString(cellValue, format || "yyyy-MM-dd HH:mm:ss");
  },
  // 四舍五入金额，每隔3位逗号分隔，默认2位数
  formatAmount({ cellValue }, digits = 2) {
    return XEUtils.commafy(XEUtils.toNumber(cellValue), { digits });
  },
  // 格式化银行卡，默认每4位空格隔开
  formatBankcard({ cellValue }) {
    return XEUtils.commafy(XEUtils.toValueString(cellValue), {
      spaceNumber: 4,
      separator: " ",
    });
  },
  // 四舍五入,默认两位数
  formatFixedNumber({ cellValue }, digits = 2) {
    return XEUtils.toFixed(XEUtils.round(cellValue, digits), digits);
  },
  // 向下舍入,默认两位数
  formatCutNumber({ cellValue }, digits = 2) {
    return XEUtils.toFixed(XEUtils.floor(cellValue, digits), digits);
  },
});

import "vxe-table-plugin-antd/dist/style.css";
VxeUI.use(VXETablePluginAntd);

//通用工具
Vue.prototype.$utils = XEUtils;

import filterModal from "@/components/eip/vxe/filterModal.vue";
VxeUI.renderer.add("MLFilterRender", {
  renderFilter(h, renderOpts, params) {
    return <filterModal params={params}></filterModal>;
  },
  //是否显示底部按钮，使用自定义的按钮
  showFilterFooter: false,
  /**
   * 筛选数据方法
   * @param {*} params
   * @returns
   */
  filterMethod(params) {
    const { option, row, column } = params;
    const { key } = option.data;
    const cellValue = row[column.field];
    return cellValue.includes(String(key));
  },
  /**
   * 重置数据方法
   * @param {*} params
   */
  filterResetMethod(params) {
    const { options } = params;
    options.forEach((option) => {
      if (option.data.options) {
        option.data.options.forEach((f) => {
          f.checked = false;
        });
      }
      option.data.write = null;
      option.data.key = null;
      option.data.op = "cn";
      if (option.data.min) {
        option.data.min = null;
      }
      if (option.data.max) {
        option.data.max = null;
      }
    });
  },
});
